package net.zhuike.novel.app.ui.fragment;


import android.content.Context;
import android.view.View;
import android.widget.ExpandableListView;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.CategoryList;
import net.zhuike.novel.app.bean.RankingList;
import net.zhuike.novel.app.common.OnRvItemClickListener;
import net.zhuike.novel.app.service.BookStoreService;
import net.zhuike.novel.app.ui.activity.SubRankActivity;
import net.zhuike.novel.app.ui.adapter.TopCategoryListAdapter;
import net.zhuike.novel.app.ui.adapter.TopRankAdapter;
import net.zhuike.novel.app.ui.base.BaseFragment;
import net.zhuike.novel.app.ui.base.Constant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import framework.common.zanetwork.core.ResponseCallback;
import okhttp3.Response;

/**
 * Created by wq on 2017/6/10.
 * 分类其他分类
 */
public class ClassificionOtherFragment extends BaseFragment{
    ExpandableListView elvFeMale;
    @Bind(R.id.elvMale)
    ExpandableListView elvMale;
    private Context context;
    private int select = 0;
    private List<RankingList.MaleBean> maleGroups = new ArrayList<>();
    private List<List<RankingList.MaleBean>> maleChilds = new ArrayList<>();
    private TopRankAdapter maleAdapter;
    private List<RankingList.MaleBean> femaleGroups = new ArrayList<>();
    private List<List<RankingList.MaleBean>> femaleChilds = new ArrayList<>();
    private TopRankAdapter femaleAdapter;

    private TopCategoryListAdapter mMaleCategoryListAdapter;
    private TopCategoryListAdapter mFemaleCategoryListAdapter;
    private List<CategoryList.Category> mMaleCategoryList = new ArrayList<>();
    private List<CategoryList.Category> mFemaleCategoryList = new ArrayList<>();
    @Override
    public int getLayoutResId() {
        return R.layout.fragment_store_other;
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void configViews() {
        context = getActivity();

       // getGender();
       // maleAdapter = new TopRankAdapter(getActivity(), maleGroups, maleChilds);
       // maleAdapter.setItemClickListener(new ClickListener());
        elvMale.setAdapter(maleAdapter);
        //分类
        //getCategoryList();
    }



    public void getGender() {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", "");
        params.put("pageSize", "");
        BookStoreService.getGender(context, params, new ResponseCallback<RankingList>() {
            @Override
            public void onSuccess(RankingList advertisesModels) {

                showRankList(advertisesModels);
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
    public void getCategoryList() {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", "");
        params.put("pageSize", "");
        BookStoreService.getCategoryList(context, "6", new ResponseCallback<CategoryList>() {
            @Override
            public void onSuccess(CategoryList advertisesModels) {

                showCategoryList(advertisesModels);
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
    public void showRankList(RankingList rankingList) {
        maleGroups.clear();
        femaleGroups.clear();
        updateMale(rankingList);
        updateFemale(rankingList);
    }
    public void showCategoryList(CategoryList data) {
        mMaleCategoryList.clear();
        mFemaleCategoryList.clear();
        //mMaleCategoryList.addAll(data.Category);
        mFemaleCategoryList.addAll(data.female);
        mMaleCategoryListAdapter.notifyDataSetChanged();
        mFemaleCategoryListAdapter.notifyDataSetChanged();
    }
    private void updateMale(RankingList rankingList) {
        List<RankingList.MaleBean> list = rankingList.subCategories;
        List<RankingList.MaleBean> collapse = new ArrayList<>();
        for (RankingList.MaleBean bean : list) {
            if (bean.collapse) { // 折叠
                collapse.add(bean);
            } else {
                maleGroups.add(bean);
                maleChilds.add(new ArrayList<RankingList.MaleBean>());
            }
        }
        if (collapse.size() > 0) {
            maleGroups.add(new RankingList.MaleBean("别人家的排行榜"));
            maleChilds.add(collapse);
        }
        maleAdapter.notifyDataSetChanged();
    }

    private void updateFemale(RankingList rankingList) {
        List<RankingList.MaleBean> list = rankingList.female;
        List<RankingList.MaleBean> collapse = new ArrayList<>();
        for (RankingList.MaleBean bean : list) {
            if (bean.collapse) { // 折叠
                collapse.add(bean);
            } else {
                femaleGroups.add(bean);
                femaleChilds.add(new ArrayList<RankingList.MaleBean>());
            }
        }
        if (collapse.size() > 0) {
            femaleGroups.add(new RankingList.MaleBean("别人家的排行榜"));
            femaleChilds.add(collapse);
        }
        femaleAdapter.notifyDataSetChanged();
    }

    class ClickListener implements OnRvItemClickListener<RankingList.MaleBean> {

        @Override
        public void onItemClick(View view, int position, RankingList.MaleBean data) {
            String allId = data.totalRank;
            if (allId == null) {
                allId = data._id;
            }
            SubRankActivity.startActivity(mContext, allId, data.title);
        }
    }
    class ClickListeners implements OnRvItemClickListener<CategoryList.Category> {

        private String gender;

        public ClickListeners(@Constant.Gender String gender) {
            this.gender = gender;
        }

        @Override
        public void onItemClick(View view, int position, CategoryList.Category data) {
            //SubCategoryListActivity.startActivity(mContext, data.name, gender);
        }
    }
}
